import { http } from "../index";

/** jsDoc
 * @typedef UserData
 * @property {String} username - 用户名
 * @property {String} password - 密码
 */

/**
 * 登录接口
 * @param {UserData} userData
 */
export const login = (userData) => {
  return http({
    url: "/users/login",
    method: "post",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
    data: userData,
  });
};

/**
 * @typedef RegisterData
 * @property {String} username - 用户名
 * @property {String} password - 密码
 * @property {String} rpassword - 重复密码
 * @property {String} nickname - 昵称
 */

/**
 * 注册接口
 * @param {RegisterData} registerData
 */
export const register = (registerData) => {
  return http({
    url: "/users/register",
    method: "post",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
    data: registerData,
  });
};

/* 
  获取用户详情信息

  支持 `restful` 风格 `localhost:8888/users/info/:id`
    即：`localhost:8888/users/info?id=xxx`
*/
export const getUserDetails = () => {
  return http({
    // url: `/users/info?id=${id}`,
    url: `/users/info`,
    method: "get",
    params: {
      id: localStorage.getItem("id"),
    },
    // headers: {
    //   authorization: localStorage.getItem("token"), // 获取到 存储在 localStorage 的 token 信息
    // },
  });
};

// 更改密码
export const changePassword = (data) =>
  http({
    url: "/users/rpwd",
    method: "post",
    data,
  });

// 调用本接口，即「登出」
export const logout = () =>
  http({
    url: `/users/logout`,
    method: "get",
    params: {
      id: localStorage.getItem("id"),
    },
  });
